package org.ank.food;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NumPadHandler {
	
	private int MAX_VALUE = 10000;
	private int RADIX = 10;
	private String backspaseButtonText;
	
	private Button[] keys;
	private TextView textView;
	private int result;
	
	public NumPadHandler(Button[] b, TextView t, Context c){
		this.keys = b;
		this.textView = t;
		this.result = 0;
		backspaseButtonText = c.getString(R.string.backspace_button_text); 
		for (int i = 0; i < b.length; i++)
			this.keys[i].setOnClickListener(listener);
		
	}
	
	public Button.OnClickListener listener = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			Button b = (Button)v;
			if ( b.getText().toString().equals(backspaseButtonText) ){
				result = (int) Math.round(Math.floor(result/10.0));
				textView.setText(String.valueOf(result));
				return;
			}
			if ( result > MAX_VALUE )
				return;
			int figure = Integer.parseInt(b.getText().toString(),RADIX);
			result = result*RADIX + figure;
			textView.setText(String.valueOf(result));
		}
		
	};
	public int getResultInt(){
		return result;
	}
}
